﻿
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link href="~/Content/layui/css/layui.css" rel="stylesheet" />
</head>
<body>
    <p>&nbsp;</p>
    <div class="layui-form-item">
        <form class="layui-form" onsubmit="return false;">
            <label class="layui-form-mid">&nbsp;&nbsp;用户名</label>
            <div class="layui-input-inline">
                <input type="text" id="UserName" name="UserName" placeholder="请输入用户名" autocomplete="off" class="layui-input" />
            </div>
            <label class="layui-form-mid">&nbsp;性别</label>
            <div class="layui-input-inline" style="width:85px;">
                <select id="Sex" name="Sex">
                    <option value="">请选择</option>
                    <option value="男">男</option>
                    <option value="女">女</option>
                </select>
            </div>
            <button class="layui-btn" id="serch" data-type="reload">搜索</button>
        </form>
    </div>
    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn " lay-event="Add">添加</button>
            <button class="layui-btn " lay-event="getCheckLength">批量删除</button>
        </div>
    </script>
    <table class="layui-hide" id="demo" lay-filter="test"></table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="updateUR">修改用户角色</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="restPwd">重置密码</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="updatePwd">修改密码</a>
    </script>

    <script src="~/Content/layui/layui.js"></script>
    <script>
        layui.use('table', function () {
            var table = layui.table;
            var $ = layui.$;

            //方法级渲染
            table.render({
                elem: '#demo'
                , url: '/User/GetUser'
                , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                , cols: [[
                    { checkbox: true, fixed: true }
                    , { field: 'UserName', title: '用户名', width: 180 }
                    , { field: 'Sex', title: '性别', width: 180 }
                    , { fixed: 'right', title: '操作', toolbar: '#barDemo', width: 400 }
                ]]
                , id: 'testReload'
                , page: true
                , height: 310

            });
            //搜素
            active = {
                reload: function () {
                    //执行重载
                    table.reload('testReload', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        , where: {
                            "UserName": $("#UserName").val(),
                            "Sex": $("#Sex").val()
                           
                        }
                    });
                }
            };
            $('#serch').on('click', function () {
                active.reload();
            });
            //头工具栏事件
            table.on('toolbar(test)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'Add':
                        layer.open({
                            title: '添加'
                            , content: '/User/Add'
                            , type: 2
                            , area: ['500px', '600px']
                        });
                        break;
                    case 'getCheckLength':
                        var arrId = "";
                        for (var i = 0; i < checkStatus.data.length; i++) {
                            arrId += checkStatus.data[i].Id + ",";
                        }
                        if (arrId != "") {
                            layer.confirm('确定删除？', function (index) {
                                $.post("/User/BatchDelArticele",
                                    { "Ids": arrId }, function (res) {
                                        if (res.Success1) {
                                            table.reload('testReload');
                                            layer.msg("删除成功！");
                                        } else {
                                            layer.msg("删除失败！");
                                        }
                                    }, 'json');
                            });

                        } else {
                            layer.msg("请选择要删除的数据");
                        }
                        break;
                    case 'isAll':
                        layer.msg(checkStatus.isAll ? '全选' : '未全选');
                        break;

                    //自定义头工具栏右侧图标 - 提示
                    case 'LAYTABLE_TIPS':
                        layer.alert('这是工具栏右侧自定义的一个图标按钮');
                        break;
                };
            });



            table.on('tool(test)', function (obj) {
                var data = obj.data;
                //console.log(obj)
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        //obj.del();
                        layer.close(index);
                        $.post("/User/Del", { id: data.Id }, function (res) {
                            if (res.Success) {
                                obj.del();
                            } else {
                                layer.alert(res.Message);
                            }
                        })
                    });
                } else if (obj.event === 'edit') {
                    layer.open({
                        title: '修改'
                        , content: '/User/Update/' + data.Id
                        , type: 2
                        , area: ['500px', '600px']
                    });
                } else if (obj.event === 'updatePwd') {
                    layer.open({
                        title: '修改密码'
                        , content: '/User/UpdatePwd/' + data.Id
                        , type: 2
                        , area: ['500px', '600px']
                    });
                } else if (obj.event === 'updateUR') {
                    layer.open({
                        title: '修改用户角色'
                        , content: '/User/UserRole/' + data.Id
                        , type: 2
                        , area: ['500px', '500px']
                    });
                } else if (obj.event === 'restPwd') {
                    $.post("/User/RestPwd", { id: data.Id }, function (res) {
                        if (res.Success) {
                            layer.alert(res.Message);
                        } else {
                            layer.alert(res.Message);
                        }
                    })
                }
            });

        });
    </script>
</body>
</html>
